package com.vision.sales.report.cl.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Ignore;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.menu.CheckMenuItem;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.vision.core.cl.ui.TabItemPanel;
import com.vision.core.cl.ui.component.EnumComboBox;
import com.vision.core.cm.PageOrientation;
import com.vision.core.cm.PageSize;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cl.ui.component.SalesmanComboBox;
import com.vision.sales.cm.data.Month;
import com.vision.sales.cm.db.data.Salesman;

/**
 * 
 * @author Mark
 *
 */
public class IndividualSalesmanSalesReportPanel extends TabItemPanel {

	interface Binder extends UiBinder<Widget, IndividualSalesmanSalesReportPanel> { }

	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer mainPanel;
	
	@UiField TextButton printBtn;
	
	@UiField TextButton sizeBtn;
	@UiField Menu sizeBtnMenu;
	@UiField CheckMenuItem letterMenuItem;
	@UiField CheckMenuItem legalMenuItem;
	@UiField CheckMenuItem a4MenuItem;
	
	@UiField TextButton orientationBtn;
	@UiField Menu orientationBtnMenu;
	@UiField CheckMenuItem portraitMenuItem;
	@UiField CheckMenuItem landscapeMenuItem;
	
	//@ UiField TextField title;
	@UiField TextField year;
	@UiField SalesmanComboBox salesman;
	@UiField SimpleComboBox<Month> month;
	//@ UiField CheckBox showZeroes;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@SuppressWarnings("deprecation")
	public IndividualSalesmanSalesReportPanel() {
		setWidget(binder.createAndBindUi(this));

		letterMenuItem.setChecked(true);
		portraitMenuItem.setChecked(true);
		
		//title.setValue("Individual Salesman Sales");
		//title.addValidator(Validators.TITLE_VALIDATOR);
		
		Date now = new Date();
		
		// set year
		DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy");
		year.setValue(fmt.format(now));

		// set month
		int index = now.getMonth() - 1;
		this.month.setValue(Month.values()[index - 1]);
	}
		
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Ignore
	@UiFactory
	SimpleComboBox<Month> initializeMonthCombo() {
		EnumComboBox<Month> cb = new EnumComboBox<Month>(Month.values());
		return cb;
	}
	
	// UiHandler Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("printBtn")
	void printClicked(SelectEvent e) {
		StringBuilder params = new StringBuilder();
		params.append("orientation=" + getOrientation());
		params.append("&size=" + getSize());
		//params.append("&title=" + title.getValue());
		params.append("&year=" + year.getValue());
		params.append("&months=" + getSelectedMonth());
		
		Salesman selectedSalesman = salesman.getValue();
		params.append("&salesman="+selectedSalesman.getId());
		
		Window.open("print/indiv_salesman_sales?" + params.toString(), null, null);
	}
	
	@UiHandler("sizeBtnMenu")
	void onSizeSelection(SelectionEvent<Item> e) {
		Item item = e.getSelectedItem();
		if (item == letterMenuItem) {
			sizeBtn.setText(PageSize.LETTER.getDisplayText());
		} else if (item == legalMenuItem) {
			sizeBtn.setText(PageSize.LEGAL.getDisplayText());
		} else if (item == a4MenuItem) {
			sizeBtn.setText(PageSize.A4.getDisplayText());
		}
	}
	
	@UiHandler("orientationBtnMenu")
	void onOrientationSelection(SelectionEvent<Item> e) {
		Item item = e.getSelectedItem();
		if (item == portraitMenuItem) {
			orientationBtn.setText(PageOrientation.PORTRAIT.getDisplayText());
		} else if (item == landscapeMenuItem) {
			orientationBtn.setText(PageOrientation.LANDSCAPE.getDisplayText());
		}
	}
	
	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@SuppressWarnings("deprecation")
	private int getSelectedMonth() {
		// default current month 
		int i = this.month.getSelectedIndex();
		if (i >= 0)		
			return i + 1;
		else
			return new Date().getMonth();
	}

	@Override
	public String getPanelId() {
		return SalesTasks.INDIV_SALESMAN_SALES.getId();
	}

	@Override
	public TabItemConfig getConfig() {
		if (config == null) {
			config = new TabItemConfig("Individual Salesman Sales Report", true);
			config.setIcon(SalesResources.INSTANCE.indivSalesmanSales());
		}
		return config;
	}


	@Override
	public void loadData() {
		salesman.load();
	}

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	PageSize getSize() {
		if (a4MenuItem.isChecked())
			return PageSize.A4;
		if (legalMenuItem.isChecked())
			return PageSize.LEGAL;
		return PageSize.LETTER;
	}
	
	PageOrientation getOrientation() {
		return portraitMenuItem.isChecked() ? PageOrientation.PORTRAIT : PageOrientation.LANDSCAPE;
	}
}
